package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.WcQuestionService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.WcQuestionDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing WcQuestion.
 */
@RestController
@RequestMapping("/api")
public class WcQuestionResource {

    private final Logger log = LoggerFactory.getLogger(WcQuestionResource.class);

    private static final String ENTITY_NAME = "wcQuestion";

    private final WcQuestionService wcQuestionService;

    public WcQuestionResource(WcQuestionService wcQuestionService) {
        this.wcQuestionService = wcQuestionService;
    }

    /**
     * POST  /wc-questions : Create a new wcQuestion.
     *
     * @param wcQuestionDTO the wcQuestionDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new wcQuestionDTO, or with status 400 (Bad Request) if the wcQuestion has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/wc-questions")
    @Timed
    public ResponseEntity<WcQuestionDTO> createWcQuestion(@RequestBody WcQuestionDTO wcQuestionDTO) throws URISyntaxException {
        log.debug("REST request to save WcQuestion : {}", wcQuestionDTO);
        if (wcQuestionDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new wcQuestion cannot already have an ID")).body(null);
        }
        WcQuestionDTO result = wcQuestionService.save(wcQuestionDTO);
        return ResponseEntity.created(new URI("/api/wc-questions/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /wc-questions : Updates an existing wcQuestion.
     *
     * @param wcQuestionDTO the wcQuestionDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated wcQuestionDTO,
     * or with status 400 (Bad Request) if the wcQuestionDTO is not valid,
     * or with status 500 (Internal Server Error) if the wcQuestionDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/wc-questions")
    @Timed
    public ResponseEntity<WcQuestionDTO> updateWcQuestion(@RequestBody WcQuestionDTO wcQuestionDTO) throws URISyntaxException {
        log.debug("REST request to update WcQuestion : {}", wcQuestionDTO);
        if (wcQuestionDTO.getId() == null) {
            return createWcQuestion(wcQuestionDTO);
        }
        WcQuestionDTO result = wcQuestionService.save(wcQuestionDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, wcQuestionDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /wc-questions : get all the wcQuestions.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of wcQuestions in body
     */
    @GetMapping("/wc-questions")
    @Timed
    public ResponseEntity<List<WcQuestionDTO>> getAllWcQuestions(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of WcQuestions");
        Page<WcQuestionDTO> page = wcQuestionService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/wc-questions");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /wc-questions/:id : get the "id" wcQuestion.
     *
     * @param id the id of the wcQuestionDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the wcQuestionDTO, or with status 404 (Not Found)
     */
    @GetMapping("/wc-questions/{id}")
    @Timed
    public ResponseEntity<WcQuestionDTO> getWcQuestion(@PathVariable Long id) {
        log.debug("REST request to get WcQuestion : {}", id);
        WcQuestionDTO wcQuestionDTO = wcQuestionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(wcQuestionDTO));
    }

    /**
     * DELETE  /wc-questions/:id : delete the "id" wcQuestion.
     *
     * @param id the id of the wcQuestionDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/wc-questions/{id}")
    @Timed
    public ResponseEntity<Void> deleteWcQuestion(@PathVariable Long id) {
        log.debug("REST request to delete WcQuestion : {}", id);
        wcQuestionService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/wc-questions?query=:query : search for the wcQuestion corresponding
     * to the query.
     *
     * @param query the query of the wcQuestion search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/wc-questions")
    @Timed
    public ResponseEntity<List<WcQuestionDTO>> searchWcQuestions(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of WcQuestions for query {}", query);
        Page<WcQuestionDTO> page = wcQuestionService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/wc-questions");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
